<?php
if(!isset($_SESSION['admincp']))exit("<script>top.location.href='http://".$_SERVER["HTTP_HOST"]."/admin/login.php'</script>");
function group_add($title,$sn,$ico,$ov,$zdgid,$roomid){
	global $db,$tablepre;
//	echo "insert into {$tablepre}auth_group(title,sn,ico,type,ov,zdgid)values('$title','$sn','$ico',0,'$ov','$zdgid')";exit;
	$db->query("insert into {$tablepre}auth_group(title,sn,ico,type,ov,zdgid,roomid)values('$title','$sn','$ico',0,'$ov','$zdgid','$roomid')");
}
function group_del($id){
	global $db,$tablepre;
	$db->query("delete from {$tablepre}auth_group where id='$id' and id not in (1,2,3)");
}
function group_edit($id,$title,$sn,$ico,$ov,$zdgid,$roomid){
	global $db,$tablepre;
	$db->query("update {$tablepre}auth_group set title='$title',sn='$sn',ico='$ico',ov='$ov',zdgid = '$zdgid',roomid = '$roomid' where id='$id'");
}

function lecturer_add($name,$remark,$ord,$videonum,$roomview,$roomid,$is_show,$ico){
	global $db,$tablepre;
	$db->query("insert into {$tablepre}lecturer(name,remark,ord,videonum,roomview,roomid,is_show,ico)values('$name','$remark','$ord','$videonum','$roomview','$roomid','$is_show','$ico')");
}

function lecturer_del($id){
	global $db,$tablepre;
	$db->query("delete from {$tablepre}lecturer where id='$id'");
}
function lecturer_edit($id,$name,$remark,$ord,$videonum,$roomview,$roomid,$is_show,$ico){
	global $db,$tablepre;
	$db->query("update {$tablepre}lecturer set name='$name',remark='$remark',ord='$ord',videonum='$videonum',roomview = '$roomview',roomid = '$roomid',is_show = '$is_show',ico = '$ico' where id='$id'");
}

function course_add($ctime,$ico){
    global $db,$tablepre;
    $time = time();
//	echo "insert into {$tablepre}course(ctime,ico,create_time)values('$ctime','$ico',$time)";exit;
    $db->query("insert into {$tablepre}course(ctime,ico,create_time)values('$ctime','$ico',$time)");
}
function setting_add($ctime,$ico){
    global $db,$tablepre;
    $time = time();
//	echo "insert into {$tablepre}setting(ctime,ico,create_time)values('$ctime','$ico',$time)";exit;
    $db->query("insert into {$tablepre}setting(ctime,ico,create_time)values('$ctime','$ico',$time)");
}

//添加禁言
function ban_word_add($uname,$ip){
    global $db,$tablepre;
    $time = time();
    $db->query("insert into {$tablepre}send(username,ip,losttime)values('$uname','$ip',$time)");
}
//充值
function charge($uid,$uname,$total){
    global $db,$tablepre;

    if(!user_exist($uname))echo '用户不存在';
    $time = time();
    $msg = $_SESSION['login_user'].'给'.$uname.'充值'.$total.'个钻石';
    //插入记录
    $a = $db->query("INSERT INTO `chat_gold_log` (`gid`, `gname`, `uid`, `uname`, `touid`, `touname`, `msg`, `dateline`, `num`, `cost`, `type`) VALUES ('', '', '$_SESSION[login_uid]', '$_SESSION[login_user]', '$uid', '$uname', '$msg', '$time', 0,'$total','2')");
    $b =  $db->query("update {$tablepre}members set  diamond = diamond + $total where username='{$uname}'");
    return $a&&$b;
}

function course_del($id){
	global $db,$tablepre;
	$db->query("delete from {$tablepre}course where id='$id'");
}
function course_edit($id,$ctime,$ico){
	global $db,$tablepre;
	$db->query("update {$tablepre}course set ctime='$ctime',ico = '$ico' where id='$id'");
}

function setting_del($id){
    global $db,$tablepre;
    $db->query("delete from {$tablepre}setting where id='$id'");
}
function setting_edit($id,$ctime,$ico){
    global $db,$tablepre;
    $db->query("update {$tablepre}setting set ctime='$ctime',ico = '$ico' where id='$id'");
}


function schedule_add($start_time,$end_time,$ico){
    global $db,$tablepre;
    $time = time();
    $start_time = strtotime($start_time);
    $end_time = strtotime($end_time);

    $db->query("insert into {$tablepre}schedule(start_time,end_time,ico,create_time)values('$start_time','$end_time','$ico',$time)");
}

function schedule_del($id){
    global $db,$tablepre;
    $db->query("delete from {$tablepre}schedule where id='$id'");
}
function schedule_edit($id,$start_time,$end_time,$ico){
    global $db,$tablepre;
    $start_time = strtotime($start_time);
    $end_time = strtotime($end_time);
    $db->query("update {$tablepre}schedule set start_time='$start_time',end_time='$end_time',ico = '$ico' where id='$id'");
}

function music_add($start_time,$end_time,$ico){
    global $db,$tablepre;
    $time = time();
    $start_time = strtotime($start_time);
    $end_time = strtotime($end_time);

    $db->query("insert into {$tablepre}music(start_time,end_time,ico,create_time)values('$start_time','$end_time','$ico',$time)");
}

function music_del($id){
    global $db,$tablepre;
    $db->query("delete from {$tablepre}music where id='$id'");
}
function music_edit($id,$start_time,$end_time,$ico){
    global $db,$tablepre;
    $start_time = strtotime($start_time);
    $end_time = strtotime($end_time);
    $db->query("update {$tablepre}music set start_time='$start_time',end_time='$end_time',ico = '$ico' where id='$id'");
}

function group_rules_edit($id,$rules){
	global $db,$tablepre;
	$db->query("update {$tablepre}auth_group set rules='$rules'  where id='$id'");
}
function user_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}members where uid  in ($ids) and uid not in (0,1)");
	$db->query("delete from {$tablepre}memberfields where uid in ($ids) and uid not in (0,1)");
}
function user_edit($id,$realname,$password,$phone,$gid,$fuser,$tuser,$sn,$state,$nickname,$rbnum,$diamond){
	global $db,$tablepre;
	if($password!="")$pwd=" ,password='".md5($password)."',";
	else $pwd=',';
	if(stripos(auth_group($_SESSION['login_gid']),'users_group')!==false)
	{
		$db->query("update {$tablepre}members set gid='$gid' where uid='$id'");
	}

	$db->query("update {$tablepre}members set realname='$realname'$pwd rbnum='$rbnum', diamond='$diamond', phone='$phone',fuser='$fuser',tuser='$tuser',state='$state' where uid='$id'");
	$db->query("update {$tablepre}memberfields set sn='$sn',nickname='$nickname' where uid='$id'");
}
/**function user_add($realname,$password,$fuser){
	global $db,$tablepre;
	if($password!="")$pwd=" ,'".md5($password)."'";
	else $pwd=',';
	$db->query("insert into {$tablepre}members(username,password,gid)values('$realname'$pwd,'1')");	
	$uid=mysql_insert_id();
	$db->query("insert into {$tablepre}memberfields (uid,nickname) values('$uid','$realname')");	
	
}*/
function user_exist($username) {
    global $db,$tablepre;
    $result = $db->query("select username from {$tablepre}members where username = '$username'");
    $member = mysql_fetch_row($result);
    if(!empty($member)) {
        return true;
    } else {
        return false;
    }
}
function user_add($username,$password,$phone,$gid,$sex,$tuser,$sn,$nickname,$rbnum,$diamond,$realname,$email){
	global $db,$tablepre,$onlineip;
	$db->query("insert into {$tablepre}members (username, password, phone, tuser, sex, realname,rbnum,diamond, email, regdate, regip, state) values('".$username."', '".md5($password)."', '".$phone."', '".$tuser."','".$sex."','".$realname."','".$rbnum."','".$diamond."','".$email."','".time()."','".$onlineip."', 1)");
	$uid=mysql_insert_id();
        if(stripos(auth_group($_SESSION['login_gid']),'users_group')!==false)
	{
		$db->query("update {$tablepre}members set gid='$gid'  where uid='$uid'");
	}
	$db->query("insert into {$tablepre}memberfields (uid,sn,nickname) values('$uid','$sn','$nickname')");	
	
}

function config_edit($arr, $roomid){
	global $db,$tablepre;
	foreach($arr as $key=>$v){
		$set[]="`$key`='$v'";
	}
	$sql="update {$tablepre}config set ".implode(",",$set)." where id = $roomid";
	$db->query($sql);
}
function config_add($arr){
	global $db,$tablepre;
	foreach($arr as $key=>$v){
		$set[]="`$key`='$v'";
	}
	$sql="insert into {$tablepre}config set ".implode(",",$set)."";
	$db->query($sql);
}
function ban_del($id){
	global $db,$tablepre;
	$db->query("delete from {$tablepre}ban where id='$id'");
}
function ban_add($username,$ip,$sn,$losttime){
	global $db,$tablepre;
	$losttime=strtotime($losttime);
	$db->query("insert into {$tablepre}ban(username,ip,sn,losttime)values('$username','$ip','$sn','$losttime')");
}
function ban_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}ban";
	if($key!="")$sql.=" where username like '%$key%' or ip like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);
}
function send_del($id){
	global $db,$tablepre;
	$db->query("delete from {$tablepre}send where id='$id'");
}
function send_add($username,$ip,$sn,$losttime){
	global $db,$tablepre;
	$losttime=strtotime($losttime);
	$db->query("insert into {$tablepre}send(username,ip,sn,losttime)values('$username','$ip','$sn','$losttime')");
}
function send_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}send";
	if($key!="")$sql.=" where username like '%$key%' or ip like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);
}
function notice_del($id){
	global $db,$tablepre;
	$db->query("delete from {$tablepre}notice where id='$id'");
}
function notice_add($title,$txt,$ov,$type){
	global $db,$tablepre;
	$losttime=strtotime($losttime);
	$db->query("insert into {$tablepre}notice(title,txt,ov,`type`)values('$title','$txt','$ov','$type')");
}
function notice_edit($id,$title,$txt,$ov,$type){
	global $db,$tablepre;
	$db->query("update {$tablepre}notice set title='$title',txt='$txt',ov='$ov',`type`='$type' where id='$id'");
}
function log_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}msgs";
	if($key!="")$sql.=" where uid like '%$key%' or ip like '%$key%' or uname like '%$key%' or msg like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);	

}
function log_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}msgs where id in ($ids)");
}
function clear_log(){
	global $db,$tablepre;
	$db->query("delete from {$tablepre}msgs");
}
function hd_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}apps_hd where id in ($ids)");
}
function app_hd_add($ktime,$ptime,$sp,$kcj,$lx,$cw,$zsj,$zyj,$username,$pcj,$sn){
	global $db,$tablepre;
	$time=gdate();
	$ktime=strtotime($ktime);
	$ptime=strtotime($ptime);
	
	$db->query("insert into {$tablepre}apps_hd(ktime,ptime,sp,kcj,lx,cw,zsj,zyj,username,pcj,sn,ttime)values('$ktime','$ptime','$sp','$kcj','$lx','$cw','$zsj','$zyj','$username','$pcj','$sn','$time')");
}
function app_hd_edit($id,$ktime,$ptime,$sp,$kcj,$lx,$cw,$zsj,$zyj,$username,$pcj,$sn){
	global $db,$tablepre;
	$ktime=strtotime($ktime);
	$ptime=strtotime($ptime);
	$db->query("update {$tablepre}apps_hd set ktime='$ktime',ptime='$ptime',sp='$sp',kcj='$kcj',lx='$lx',cw='$cw',zsj='$zsj',zyj='$zyj',username='$username',pcj='$pcj',sn='$sn' where id='$id'");
}
function app_hd_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}apps_hd";
	if($key!="")$sql.=" where uname like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
		while($row=$db->fetch_row($query)){
		$t=$tpl;
		
		if(strpos($row[lx],'买')&&$row['pcj']!=""){
			$t=str_replace('{yld}',round($row['pcj']-$row['kcj'],2),$t);
		}
		else if(strpos($row[lx],'卖')&&$row['pcj']!=""){
			$t=str_replace('{yld}',round($row['kcj']-$row['pcj'],2),$t);
		}else{
			$t=str_replace('{yld}','',$t);
		}
		foreach($row as $k=>$value){
			$t=str_replace('{'.$k.'}',$value,$t);	
		}
		$str.=$t;
		
	}
	return $str;

}

function app_wt_edit($id,$q,$a,$quser,$auser,$zt){
	global $db,$tablepre;
	$db->query("update {$tablepre}apps_wt set q='$q',a='$a',quser='$quser',auser='$auser',zt='$zt'  where id='$id'");
}
function app_wt_add($q,$a,$quser,$auser,$zt){
	global $db,$tablepre;
	$qtime=gdate();
	$db->query("insert into {$tablepre}apps_wt(q,a,quser,auser,qtime,zt)values('$q','$a','$quser','$auser','$qtime','$zt')");
}
function wt_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}apps_wt where id in ($ids)");
}
function app_wt_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}apps_wt";
	if($key!="")$sql.=" where q like '%$key%' or a like '%$key%' or quser like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);	

}


function app_jyts_edit($id,$title,$txt,$user){
	global $db,$tablepre;
	$db->query("update {$tablepre}apps_jyts set title='$title',txt='$txt',`user`='$user' where id='$id'");
}
function app_jyts_add($title,$txt,$auser){
	global $db,$tablepre;
	$atime=gdate();
	$db->query("insert into {$tablepre}apps_jyts(title,txt,`user`,atime)values('$title','$txt','$user','$atime')");
}
function jyts_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}apps_jyts where id in ($ids)");
}
function app_jyts_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}apps_jyts";
	if($key!="")$sql.=" where title like '%$key%' or txt like '%$key%' or `user` like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);	

}


function app_scpl_edit($id,$title,$txt,$user,$dj){
	global $db,$tablepre;
	$db->query("update {$tablepre}apps_scpl set title='$title',txt='$txt',`user`='$user',dj='$dj' where id='$id'");
}
function app_scpl_add($title,$txt,$user,$jd){
	global $db,$tablepre;
	$atime=gdate();
	$db->query("insert into {$tablepre}apps_scpl(title,txt,`user`,atime,dj)values('$title','$txt','$user','$atime','$dj')");
}
function scpl_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}apps_scpl where id in ($ids)");
}
function app_scpl_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}apps_scpl";
	if($key!="")$sql.=" where title like '%$key%' or txt like '%$key%' or `user` like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);	

}



function app_files_edit($id,$title,$url,$user){
	global $db,$tablepre;
	$db->query("update {$tablepre}apps_files set title='$title',url='$url',`user`='$user' where id='$id'");
}
function app_files_add($title,$url,$user){
	global $db,$tablepre;
	$atime=gdate();
	$db->query("insert into {$tablepre}apps_files(title,url,`user`,atime)values('$title','$url','$user','$atime')");
}
function files_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}apps_files where id in ($ids)");
}
function app_files_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}apps_files";
	if($key!="")$sql.=" where title like '%$key%'   or `user` like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);	

}




function app_manage_edit($id,$title,$url,$ico,$w,$h,$target,$s,$ov){
	global $db,$tablepre;
	$db->query("update {$tablepre}apps_manage set title='$title',url='$url',ico='$ico',w='$w',h='$h',target='$target',s='$s',ov='$ov' where id='$id'");
}
function header_manage_edit($id,$title,$url,$ico,$w,$h,$target,$s,$ov){
	global $db,$tablepre;
	$db->query("update {$tablepre}header_manage set title='$title',url='$url',ico='$ico',w='$w',h='$h',target='$target',s='$s',ov='$ov' where id='$id'");
}
function app_manage_add($title,$url,$ico,$w,$h,$target,$s,$ov){
	global $db,$tablepre;
	$atime=gdate();
	$db->query("insert into {$tablepre}apps_manage(title,url,ico,w,h,target,s,ov)values('$title','$url','$ico','$w','$h','$target','$s','$ov')");
}
function header_manage_add($title,$url,$ico,$w,$h,$target,$s,$ov){
	global $db,$tablepre;
	$atime=gdate();
	$db->query("insert into {$tablepre}header_manage(title,url,ico,w,h,target,s,ov)values('$title','$url','$ico','$w','$h','$target','$s','$ov')");
}
function manage_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}apps_manage where id in ($ids) and id not in(1,2,3,4,5,6)");
}
function header_manage_del($ids){
	global $db,$tablepre;
	if($ids=="")return;
	$db->query("delete from {$tablepre}header_manage where id in ($ids)");
}
function app_manage_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}apps_manage";
	if($key!="")$sql.=" where title like '%$key%'   or `user` like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by s,ov desc,id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);	

}
function header_manage_list($num,$key,$tpl){
	global $db,$tablepre,$firstcount,$displaypg;
	$sql="select * from {$tablepre}header_manage";
	if($key!="")$sql.=" where title like '%$key%'   or `user` like '%$key%'";
	
	$count=$db->num_rows($db->query($sql));
	pageft($count,$num,"");
	$sql.=" order by s,ov desc,id desc";
	$sql.=" limit $firstcount,$displaypg";
	$query=$db->query($sql);
	return for_each($query,$tpl);	

}
function curl_get($get_data, $url, $base_url){
	$url = $base_url . "/"  . $url;
	$params_str = http_build_query($get_data);
	$url = $url . "?" . $params_str;
	$ch = curl_init($url) ;
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
	curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
	curl_setopt($ch, CURLOPT_TIMEOUT, 20) ;
	// 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
	$output = curl_exec($ch) ;
	return $output;
}
function curl_post($post_data, $url, $base_url){
	$url = $base_url . "/"  . $url;
//	dump($url);exit;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	// post数据
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 20) ;
	// post的变量
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	$output = curl_exec($ch);
	curl_close($ch);
	return $output;
}
?>